cmake_minimum_required(VERSION 3.26)
project(Demo1_1)  # 请将 MyProject 替换为您的项目名称

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 启用自动 MOC 和 RRC 、UIC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
SET(CMAKE_AUTOUIC ON)

# 设置 Qt 安装路径（请根据实际情况修改）
#set(CMAKE_PREFIX_PATH "D:/Lib/C++/QT/6.8.3/6.8.3/msvc2022_64" CACHE PATH "Path to Qt installation")

# 查找 Qt 组件
find_package(Qt6 COMPONENTS
        Core
        Qml
        Quick
        QuickControls2
        REQUIRED
)

# 添加可执行文件
add_executable(${PROJECT_NAME}
        resource/resource.qrc
        main.cpp
)

# 链接 Qt 库
target_link_libraries(${PROJECT_NAME}
        Qt::Core
        Qt::Qml
        Qt::Quick
        Qt::QuickControls2
)

# 取消命令黑框
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE TRUE)

# 设置 QML 文件路径（请根据实际情况修改）
set(QML_DIR "resource/qml" CACHE PATH "Path to Qml")
if (NOT QML_DIR)
    set(QML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/resource/qml")
endif ()

# 打印调试信息
message(STATUS "QML_DIR: ${QML_DIR}")

# 利用 QT windeployqt.exe 查找应用程序需要的 QT 库发布程序
if (WIN32)
    # 获取 windeployqt.exe 所在完整路径
    set(WIN_DEPLOY_QT "${CMAKE_PREFIX_PATH}/bin/windeployqt.exe")
    if (EXISTS ${WIN_DEPLOY_QT})
        # 相当于执行了 windeployqt.exe --qmldir "项目路径/qml"  "XXXX.exe"
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E echo "Running windeployqt with QML_DIR: ${QML_DIR}"
                COMMAND ${WIN_DEPLOY_QT}
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}.exe"
                --qmldir "${CMAKE_CURRENT_SOURCE_DIR}/${QML_DIR}"
                COMMENT "Running windeployqt..."
        )
    else ()
        message(SEND_ERROR "windeployqt tool not found. Please check your Qt installation.")
    endif ()
endif ()